import urllib.request
from lxml import etree


def fetch_spell_config(spell_id: int):
    url: str = 'http://www.wowhead.com/spell=%d' % (spell_id,)
    response = urllib.request.urlopen(url)  # 打开url地址,并返回一个 HTTPRespone实例
    html = response.read().decode('utf-8')  # 调用实例的 read()方法，并用 utf-8进行解码处理。就得出完整的百度的HTML文件
    # print(html)
    root = etree.HTML(html)  # type:etree._Element
    xpath_spell_name = "//div[@class='text']/noscript/table/tr/td/b/text()"
    td_spell_name: etree._Element = root.xpath(xpath_spell_name)
    spell_name = td_spell_name[0]
    print("get spell en name: %s" % spell_name)
    xpath_gcd = "//div[@class='text']/table[@class='grid auto-width']/tr[6]/td/text()"
    td_gcd: etree._Element = root.xpath(xpath_gcd)
    data_split = str(td_gcd[0]).split()
    spell_gcd = float(data_split[0])
    print("get spell gcd : %f" % (spell_gcd,))
    url_cn: str = 'http://cn.wowhead.com/spell=%d' % (spell_id,)
    response = urllib.request.urlopen(url_cn)  # 打开url地址,并返回一个 HTTPRespone实例
    html = response.read().decode('utf-8')  # 调用实例的 read()方法，并用 utf-8进行解码处理。就得出完整的百度的HTML文件
    # print(html)
    root = etree.HTML(html)  # type:etree._Element
    xpath_spell_name = "//div[@class='text']/noscript/table/tr/td/b/text()"
    td_spell_cn_name: etree._Element = root.xpath(xpath_spell_name)
    spell_cn_name = td_spell_cn_name[0]
    print("get spell cn name: %s" % spell_cn_name)
    return spell_gcd, spell_name, spell_cn_name


def main():
    fetch_spell_config(57755)


if __name__ == '__main__':
    main()
